#!/usr/bin/env python3
# Testing stuff.  This should probably eventually be moved to a TestSuite.

import datetime
import importlib
import logging
import unittest
import forecasts

class TestForecastModules(unittest.TestCase):

    def __init__(self, testname, module):
        super().__init__(testname)
        self.module = module

    def setUp(self):
        self.fgen = self.module.ForecastGen(51.54999924, 5.11666679)
        self.fgen.shutup()

    def test_url_generated(self):
        self.assertNotEqual(self.fgen.url, '')

    def test_name_set(self):
        self.assertNotEqual(self.fgen.shortname, 'common')

    def test_output(self):
        # too much in one test, but I really don't want to call the API over 
        # and over
        one = self.fgen.getforecast(1)
        two = self.fgen.getforecast(2)
        four = self.fgen.getforecast(4)
        self.forecast_sane(one)
        self.forecast_sane(two)
        self.forecast_sane(four)

    def forecast_sane(self, forecast):
        self.assertIsInstance(forecast.date_forecasted, datetime.date)
        self.assertIsInstance(forecast.date_observed, datetime.date)
        self.assertLessEqual(forecast.date_observed, forecast.date_forecasted)
        self.assertGreater(forecast.t_min, -274)
        self.assertGreater(forecast.t_max, -274)
        self.assertLessEqual(forecast.t_min, forecast.t_max)

if __name__ == '__main__':
    logging.basicConfig(format='%(levelname)s-%(name)s: %(message)s', level=logging.DEBUG)
    for modname in forecasts.modules:
        print('Running tests on forecasts.' + modname)
        suite = unittest.TestSuite()
        module = importlib.import_module('forecasts.' + modname)
        suite.addTest(TestForecastModules('test_name_set', module))
        suite.addTest(TestForecastModules('test_url_generated', module))
        suite.addTest(TestForecastModules('test_output', module))
        unittest.TextTestRunner(verbosity=2).run(suite)